/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Text;
using VSTrid = Microsoft.VisualStudio.VirtualTreeGrid;
using System.Reflection;

namespace DslEditorPowerToy.Controls.VirtualTreeGrid.Branches
{
    public class ColumnDescriptorCollection : List<ColumnDescriptor>
    {
        #region Constructors
        public ColumnDescriptorCollection() : base()
        {
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets the maximum column order
        /// </summary>
        /// <returns></returns>
        public int MaxColumnOrder
        {
            get
            {
                //Get the highest column index
                int maxColumnIndex = 1;
                foreach (ColumnDescriptor descriptor in this)
                {
                    if (descriptor.ColumnOrder > maxColumnIndex)
                        maxColumnIndex = descriptor.ColumnOrder;
                }

                //Ensure no overflow
                if (maxColumnIndex < 1)
                    return 1;

                return maxColumnIndex;
            }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Sorts the collection according to the column order
        /// </summary>
        public new void Sort()
        {
            base.Sort(new ColumnDescriptorOrderComparer());
        }
        /// <summary>
        /// Returns the column by specified column order
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        public ColumnDescriptor GetByColumnOrder(int columnOrder)
        {
            if ((columnOrder > this.MaxColumnOrder) 
                || (columnOrder < 1))
                return null;

            foreach (ColumnDescriptor descriptor in this)
            {
                if (descriptor.ColumnOrder == columnOrder)
                    return descriptor;
            }

            return null;
        }
        #endregion
    }
    /// <summary>
    /// Comparer for column propertyDescriptor order
    /// </summary>
    public class ColumnDescriptorOrderComparer : IComparer<ColumnDescriptor>
    {
        #region Constructors
        public ColumnDescriptorOrderComparer()
        {
        }
        #endregion

        #region IComparer Members

        /// <summary>
        /// Compares two given columnDescriptors based upon the columnOrder
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public int Compare(ColumnDescriptor x, ColumnDescriptor y)
        {
            if ((null == x) || (null == y))
                return 0;

            //Compare orders
            if (x.ColumnOrder == y.ColumnOrder)
                return 0;
            if (x.ColumnOrder < y.ColumnOrder)
                return -1;
            if (x.ColumnOrder > y.ColumnOrder)
                return 1;

            return int.MinValue;
        }

        #endregion
    }

}